package com.yfp.operation.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yfp.operation.domain.DeviceCurrentComboPlanRoadDO;
import com.yfp.operation.domain.DeviceCurrentComboStockDO;
import com.yfp.operation.domain.dto.DamageDTO;
import com.yfp.operation.mapper.DeviceCurrentComboStockMapper;
import com.yfp.operation.service.DeviceCurrentComboPlanRoadService;
import com.yfp.operation.service.DeviceCurrentComboStockService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 *
 * @description 针对表【device_current_combo_stock】的数据库操作Service实现
 * @createDate 2024-04-24 15:36:52
 */
@Service
public class DeviceCurrentComboStockServiceImpl extends ServiceImpl<DeviceCurrentComboStockMapper, DeviceCurrentComboStockDO> implements DeviceCurrentComboStockService {

    @Resource
    private DeviceCurrentComboPlanRoadService deviceCurrentComboPlanRoadService;

    @Resource
    private DeviceCurrentComboStockMapper deviceCurrentComboStockMapper;


    /**
     * 获取当前设备下所有套餐
     *
     * @param damageDTOS
     * @return
     */
    @Override
    public List<DeviceCurrentComboStockDO> listStock(List<DamageDTO> damageDTOS) {
        DamageDTO first = CollUtil.getFirst(damageDTOS);
        LambdaQueryWrapper<DeviceCurrentComboStockDO> query = Wrappers.lambdaQuery(DeviceCurrentComboStockDO.class);
        query.eq(DeviceCurrentComboStockDO::getCPlanId, first.getCPlanId());
        query.eq(DeviceCurrentComboStockDO::getMachineId, first.getMachineId());
        return this.list(query);
    }

    /**
     * 报损处理
     *
     * @param damageDTOS
     * @return
     */
    @Transactional
    @Override
    public List<DeviceCurrentComboPlanRoadDO> damageHandle(List<DamageDTO> damageDTOS) {
        List<DeviceCurrentComboStockDO> deviceCurrentComboStockDOS = this.listStock(damageDTOS);

        List<String> roadNumberList = damageDTOS.stream().map(DamageDTO::getRoadNumber).collect(Collectors.toList());
        LambdaQueryWrapper<DeviceCurrentComboPlanRoadDO> query = Wrappers.lambdaQuery(DeviceCurrentComboPlanRoadDO.class);
        query.eq(DeviceCurrentComboPlanRoadDO::getMachineId, damageDTOS.get(0).getMachineId());
        query.in(DeviceCurrentComboPlanRoadDO::getRoadNumber, roadNumberList);
        List<DeviceCurrentComboPlanRoadDO> deviceCurrentComboPlanRoadDOS = deviceCurrentComboPlanRoadService.list(query);

        Map<String, DamageDTO> damageByRoadNumber =damageDTOS.stream().collect(Collectors.toMap(DamageDTO::getRoadNumber, item -> item, (oldValue, newValue) -> oldValue));
        for (DeviceCurrentComboPlanRoadDO deviceCurrentComboPlanRoadDO : deviceCurrentComboPlanRoadDOS) {
            DamageDTO damageDTO = MapUtil.get(damageByRoadNumber, deviceCurrentComboPlanRoadDO.getRoadNumber(), DamageDTO.class);
            BigDecimal comboNum = NumberUtil.sub(deviceCurrentComboPlanRoadDO.getComboNum(), damageDTO.getComboNum().toString());
            deviceCurrentComboPlanRoadDO.setComboNum(comboNum.toString());
        }

        // 获取报损套餐的数量
        Map<String, Integer> comboNumMapByComboId = deviceCurrentComboPlanRoadDOS.stream()
                .collect(Collectors.groupingBy(DeviceCurrentComboPlanRoadDO::getComboId))
                .entrySet().stream().collect(Collectors.toMap(
                        entry -> entry.getKey(),
                        entry -> entry.getValue().stream()
                                .map(item -> Integer.valueOf(item.getComboNum()))
                                .mapToInt(Integer::intValue)
                                .sum()
                ));
        comboNumMapByComboId.forEach((key, value) -> {
            if (deviceCurrentComboStockDOS.stream().anyMatch(deviceCurrentComboStockDO -> deviceCurrentComboStockDO.getComboId().equals(key))) {
                DeviceCurrentComboStockDO deviceCurrentComboStockDO = deviceCurrentComboStockDOS.stream().filter(deviceCurrentComboStockDO1 -> deviceCurrentComboStockDO1.getComboId().equals(key)).findFirst().get();
                deviceCurrentComboStockDO.setComboNum(deviceCurrentComboStockDO.getComboNum() - value);
                if (deviceCurrentComboStockDO.getComboNum() < 0) {
                    deviceCurrentComboStockDO.setComboNum(0);
                }
                this.updateById(deviceCurrentComboStockDO);
            }
        });
        return deviceCurrentComboPlanRoadDOS;
    }

    @Override
    public DeviceCurrentComboStockDO selectByMachineIdAndComboId(String machineId, String comboId) {
        return deviceCurrentComboStockMapper.selectByMachineIdAndComboId(machineId, comboId);
    }

    @Transactional
    @Override
    public void deleteByMachineId(String deviceId) {
        deviceCurrentComboStockMapper.deleteByMachineId(deviceId);
    }

    @Transactional
    @Override
    public void insert(DeviceCurrentComboStockDO v) {
        deviceCurrentComboStockMapper.insert(v);
    }

    @Transactional
    @Override
    public void updateStock(DeviceCurrentComboStockDO aDo) {
        deviceCurrentComboStockMapper.updateStock(aDo);
    }

    @Transactional
    @Override
    public void batchUpdate(ArrayList<DeviceCurrentComboStockDO> dccsList) {
        deviceCurrentComboStockMapper.batchUpdate(dccsList);
    }

    @Override
    public void insertAll(List<DeviceCurrentComboStockDO> dccsList) {
        deviceCurrentComboStockMapper.insertAll(dccsList);
    }

    /**
     * 获取当前线上库存
     * @param machineId
     * @return
     */
    @Override
    public List<DeviceCurrentComboStockDO> getByMacineId(String machineId) {
        return deviceCurrentComboStockMapper.getByMacineId(machineId);
    }
}




